Hello again....
I have a a WebDropdown that has been loaded with data from a table. What are the properties that I need to use to get and restore the selected item in the WebDropdown.
Hi,
The selected item is automatically stored in the SelectedItem WebDropDown property. If you have EnableMultipleSelection=true, you need to use the SelectedItems collection :
1)
WebDropDown1.SelectedItem
2)
WebDropDown1.SelectedItems
You do not need any special code / logic to store and restore the selection across postbacks - it is done automatically.
In order to set an Item as selected, you can do :
WebDropDown1.Items[0].Selected = true; // this will mark the first item as selected
For single selection you can also do :
WebDropDown1.SelectedItem = item;
Thanks,
Angel
Here is the code for th control. I still cant get it to restore the selected item from the saved value field that was saved in the database. I was Using WebCombo. I used [ControlName].DataValue to get the IRecId and [ControlName].FindByValue([Var int32]).Selected = True to restore the IRecId data. These properties are not in WebDropDown.
<ig:WebDropDown ID="IndustryDdl2" runat="server" AutoFilterResultSize="30" AutoFilterSortOrder="Ascending" DataKeyFields="IRecId" DataSourceID="dsIndustries" DropDownAnimationType="Linear" DropDownContainerHeight="50px" DropDownContainerMaxHeight="100px" DropDownContainerWidth="150px" EnableDropDownAsChild="True" Height="18px" MultipleSelectionType="Checkbox" PageSize="0" TextField="IndustryName" ValueField="IRecId" Width="150px" DropDownAnimationDuration="250"><DropDownItemBinding TextField="IndustryName" ValueField="IRecId" /><Button ImageUrl="~/Images/WDdl.gif" pressedimageurl="~/Images/WDdl_Up.gif" /></ig:WebDropDown>
Hi ,
As far as I understand your scenario (please correct me if I am wrong) :
1) the user selects some item
2) some postback takes place
3) you store the value field in the database , if it wasn't there before
4) after some time, you would like, on a subsequent load of the page, to have the same item selected ?
In the dropdown, the property that corresponds to the SelectedItem's value field is called SelectedValue (same naming as in Microsoft controls ) , i.e. if you have an Item with Text="abc 123" and Value="Id123", SelectedValue will be "abc123". WebCombo's DataValue is equivalent to the WebDropDown's SelectedValue.
In order to find the item corresponding to a specific data value (i.e. SelectedValue property contents), you can do the following (very similar to the Webcombo code):
DropDownItem item = [ControlName].Items.FindItemByValue([ControlName].SelectedValue);
item.Selected = true;
Hope this helps. Please let me know if I can help with anything else. Thanks,
Yep, the scenario is correct.
The WebDropdown.SelectedValue is returning the correct integer for me to save, but the WebDropdown.Items.FindItemByValue([int database value]) is not selecting the correct item. I have tryed
WebDropdown.Items.FindItemByValue([int database value]).Selected = True,
WebDropdown.Items.FindItemByValue([int database value].ToString)
WebDropdown.Items.FindItemByValue([int database value].ToString).Selected = true
Eventho the control is loaded from SqlDataSource on the form, I have to reload it, (WebControl.DataBind()), before I do the FindByValue. If I dont, I get the error:
System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." Source="App_Web_lqs9qdse" StackTrace: at Accounts.LoadAccountData(Int32 RecId) in C:\Documents and Settings\Jeff\My Documents\Visual Studio 2008\WebSites\Valiqore2\Accounts.aspx.vb:line 240 at Accounts.AccountsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) in C:\Documents and Settings\Jeff\My Documents\Visual Studio 2008\WebSites\Valiqore2\Accounts.aspx.vb:line 33 at System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e) at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean c***alidation, String validationGroup) at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:
Could the above error be the cause of the FindByValue not working ?
Thanks for all the help on this.